﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace ByteSite.Common
{
    public class ExceptionHelper : Exception
    {

        private string ErrMsg;
        private string ErrContent;
        private string ErrUrl;
        private string ErrTitle;
        private string ErrLine;


        public ExceptionHelper() : base()
        { }

        public ExceptionHelper(string message)
            : base(message)
        {
            ErrUrl = HttpContext.Current.Request.Url.ToString();
            ErrTitle = "系统错误：" + message;

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html><head><title>ThintenError</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><style type=\"text/css\">body{background-color:white;color:black;font:9pt/11pt verdana,arial,sans-serif;}#container{width:100%;}#message{width:100%;color:black;}.red{color:red;}a:link{font:9pt/11pt verdana,arial,sans-serif;color:red;}a:visited{font:9pt/11pt verdana,arial,sans-serif;color:#4e4e4e;}h1{color:#FF0000;font:18pt \"Verdana\";margin-bottom:0.5em;}.bg1{background-color:#FFFFCC;}.bg2{background-color:#EEEEEE;}.table{background:#AAAAAA;font:11pt/14pt Menlo,Consolas,\"LucidaConsole\"}.table td{padding:5px;}.info{background:none repeat scroll 0 0 #F3F3F3;border:0px solid #aaaaaa;border-radius:10px 10px 10px 10px;color:#000000;font-size:11pt;line-height:160%;margin-bottom:1em;padding:1em;}.help{background:#F3F3F3;border-radius:10px 10px 10px 10px;font:12px verdana,arial,sans-serif;text-align:center;line-height:160%;padding:1em;}.sql{background:none repeat scroll 0 0 #FFFFCC;border:1px solid #aaaaaa;color:#000000;font:arial,sans-serif;font-size:9pt;line-height:160%;margin-top:1em;	padding:4px;}</style></head><body><div id=\"container\"><h1>Thinten Error</h1><div class='info'>" + ErrTitle + "</div><div class=\"info\"><p><strong>Asp.net Debug</strong></p><table cellpadding=\"2\" cellspacing=\"1\" width=\"100%\" class=\"table\"><tr class=\"bg2\"><td width=\"120\"><strong>项目</strong></td><td><strong>信息</strong></td></tr><tr class=\"bg1\"><td>错误页面</td><td>" + ErrUrl + "</td></tr></table></div><div class=\"help\">系统已经将此出错信息详细记录,由此给您带来的访问不便我们深感歉意.&nbsp;&nbsp;</div></div></body></html>");
            HttpContext.Current.Response.End();
            return;
        }


        public ExceptionHelper(string message, Exception exception)
            : base(message, exception)
        {
            //后期建立错误资源代码库

            ErrUrl = HttpContext.Current.Request.Url.ToString();
            ErrMsg = exception.Message;
            ErrContent = exception.StackTrace.Replace("\r\n", "<br />");
            ErrTitle = exception.Message + "(可能是由于“" + message + "”导致的)";

            //定义正则表达式用来匹配标签
            Regex regLine = new Regex(@"位置(.?)*行号(.?)*(\d)", RegexOptions.IgnoreCase);

            //搜索匹配的字符串
            MatchCollection matches = regLine.Matches(exception.StackTrace);
            if (matches.Count > 0)
            {
                foreach (Match match in matches)
                {
                    ErrLine += match.Value;
                }
            }
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html><head><title>ThintenError</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><style type=\"text/css\">body{background-color:white;color:black;font:9pt/11pt verdana,arial,sans-serif;}#container{width:100%;}#message{width:100%;color:black;}.red{color:red;}a:link{font:9pt/11pt verdana,arial,sans-serif;color:red;}a:visited{font:9pt/11pt verdana,arial,sans-serif;color:#4e4e4e;}h1{color:#FF0000;font:18pt \"Verdana\";margin-bottom:0.5em;}.bg1{background-color:#FFFFCC;}.bg2{background-color:#EEEEEE;}.table{background:#AAAAAA;font:11pt/14pt Menlo,Consolas,\"LucidaConsole\"}.table td{padding:5px;}.info{background:none repeat scroll 0 0 #F3F3F3;border:0px solid #aaaaaa;border-radius:10px 10px 10px 10px;color:#000000;font-size:11pt;line-height:160%;margin-bottom:1em;padding:1em;}.help{background:#F3F3F3;border-radius:10px 10px 10px 10px;font:12px verdana,arial,sans-serif;text-align:center;line-height:160%;padding:1em;}.sql{background:none repeat scroll 0 0 #FFFFCC;border:1px solid #aaaaaa;color:#000000;font:arial,sans-serif;font-size:9pt;line-height:160%;margin-top:1em;	padding:4px;}</style></head><body><div id=\"container\"><h1>Thinten Error</h1><div class='info'>" + ErrTitle + "</div><div class=\"info\"><p><strong>Asp.net Debug</strong></p><table cellpadding=\"2\" cellspacing=\"1\" width=\"100%\" class=\"table\"><tr class=\"bg2\"><td width=\"120\"><strong>项目</strong></td><td><strong>信息</strong></td></tr><tr class=\"bg1\"><td>错误代码</td><td>" + ErrCode(ErrMsg) + "</td></tr><tr class=\"bg1\"><td>错误位置</td><td>" + ErrLine + "</td></tr><tr class=\"bg1\"><td>错误页面</td><td>" + ErrUrl + "</td></tr><tr class=\"bg1\"><td>异常信息</td><td>" + ErrMsg + "</td></tr><tr class=\"bg1\"><td colspan=\"2\">异常信息:<br/>" + ErrContent + "</td></tr></table></div><div class=\"help\"><a href=\"http://www.thinten.com\">讯通公司</a>&nbsp;&nbsp;已经将此出错信息详细记录,由此给您带来的访问不便我们深感歉意.&nbsp;&nbsp;<a href=\"http://www.baidu.com/s?wd=" + ErrMsg + "\" target=\"_blank\"><span class=\"red\">在线求助?</span></a></div></div></body></html>");
            HttpContext.Current.Response.End();
            return;
        }

        public string ErrCode(string msg)
        {
            string code = "";
            switch (msg)
            {
                case "输入字符串的格式不正确。": code = "000001"; break;
                default: code = "000000"; break;
            }
            return code;
        }

        public string Solution(string code)
        {
            string html = "";
            switch (code)
            {
                case "000000": html = "<a href=\"#\">未知错误</a>"; break;
                case "000001": html = "<a href=\"#\">字符串格式错误，请检查字符串处理前后的变量类型</a>"; break;
                default: html = "<a href=\"#\">未知错误</a>"; break;
            }
            return html;
        }
    }
}
